package com.zoo.diannaopeijian.controller.shipment;

import com.zoo.diannaopeijian.common.page.Page;
import com.zoo.diannaopeijian.controller.BaseController;
import com.zoo.diannaopeijian.domain.ShipmentItem;
import com.zoo.diannaopeijian.service.ShipmentItemService;
import com.zoo.diannaopeijian.service.ShipmentRecordService;
import com.zoo.diannaopeijian.service.ShipmentService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 出货控制器
 */
@Controller
@RequestMapping("/shipment")
public class ShipmentController extends BaseController {
    @Resource
    private ShipmentService shipmentService;
    @Resource
    private ShipmentRecordService shipmentRecordService;
    @Resource
    private ShipmentItemService shipmentItemService;

    @GetMapping(value = "/pages/{pageName}")
    public String pageRoute(@PathVariable String pageName) {
        return "shipment/" + pageName;
    }
//
//    @RequestMapping("exportPage")
//    public String exportPageRoute() {
//        return "export/exportPage";
//    }
//
//    @RequestMapping("ReexportPage")
//    public String ReexportPageRoute() {
//        return "export/ReexportPage";
//    }
//
//    @RequestMapping("exportRecordPage")
//    public String exportRecordPageRoute() {
//        return "/export/exportRecordPage";
//    }
//
//    @RequestMapping("exportItemPage")
//    public String exportItemPageRoute() {
//        return "/export/exportItemPage";
//    }

    @PostMapping(value = "/shipmentRecord")
    public ResponseEntity<String> shipmentWares(@RequestBody List<ShipmentItem> shipmentItemList) {
        return ResponseEntity.ok(shipmentService.shipmentWares(getLoginId(), shipmentItemList) ? success : fail);
    }

    @PutMapping(value = "/shipmentRecord")
    public ResponseEntity<String> reShipmentWares(@RequestBody List<ShipmentItem> shipmentItemList) {
        Integer shipmentRecordId = shipmentItemList.get(0).getShiptRecId();

        if (shipmentItemList.get(0).getWareId() == null) {
            shipmentItemList.clear();
        }

        return ResponseEntity.ok(shipmentService.reShipmentWares(shipmentItemList, shipmentRecordId) ? success : fail);
    }

    @GetMapping("/shipmentRecord")
    public ResponseEntity<Map<String, Object>> getExportRecord(
            Page page,
            @DateTimeFormat(pattern  = "yyyy-MM-dd") Date from,
            @DateTimeFormat(pattern  = "yyyy-MM-dd") Date to,
            String staffName
    ) {
        return ResponseEntity.ok(buildPageResult(shipmentRecordService.getShipmentRecordWithPage(from, to, staffName, page)));
    }

    @GetMapping("/shipmentItem")
    public ResponseEntity<List<Map<String, Object>>> shipmentItemGet(Integer shipmentRecordId) {
        return ResponseEntity.ok(shipmentItemService.getShipmentItemsByShiptRecordId(shipmentRecordId));
    }
}
